/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import cn.hg.jfsa.core.option.Options;
import cn.hg.jfsa.core.service.OptionServiceApi;
import cn.hg.jfsa.model.JfsaOption;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Golly
 *
 */
public class JfsaOptionService implements OptionServiceApi {
	@Override
	public Map<String, String> getAll() {
		List<JfsaOption> settingList = JfsaOption.DAO.find("SELECT `key`, `value` FROM jfsa_option");
		Map<String, String> optionMap = new HashMap<>();
		for (JfsaOption setting : settingList) {
			optionMap.put(setting.getKey(), setting.getValue());
		}
		return optionMap;
	}

	public boolean setOption(String key, String value) {
		JfsaOption option = JfsaOption.DAO.findFirst("SELECT * FROM jfsa_option WHERE `key`=?", key);
		if (null == option) {
			return false;
		}
		boolean act = option.setValue(value).update();

		if (act) {
			Options.set(key, value);
		}

		return act;
	}
}
